<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>MiXiM: LMacLayer Class Reference</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">MiXiM
   &#160;<span id="projectnumber">2.3</span>
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li class="current"><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="dirs.html"><span>Directories</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="annotated.html"><span>Class&#160;List</span></a></li>
      <li><a href="classes.html"><span>Class&#160;Index</span></a></li>
      <li><a href="inherits.html"><span>Class&#160;Hierarchy</span></a></li>
      <li><a href="functions.html"><span>Class&#160;Members</span></a></li>
    </ul>
  </div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('a00126.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="summary">
<a href="#pub-methods">Public Member Functions</a> &#124;
<a href="#pro-types">Protected Types</a> &#124;
<a href="#pro-methods">Protected Member Functions</a> &#124;
<a href="#pro-attribs">Protected Attributes</a> &#124;
<a href="#pro-static-attribs">Static Protected Attributes</a> &#124;
<a href="#pri-methods">Private Member Functions</a>  </div>
  <div class="headertitle">
<div class="title">LMacLayer Class Reference<div class="ingroups"><a class="el" href="a00509.html">macLayer - MAC layer modules</a></div></div>  </div>
</div>
<div class="contents">
<!-- doxytag: class="LMacLayer" --><!-- doxytag: inherits="BaseMacLayer" -->
<p>Implementation of L-MAC (Lightweight Medium Access Protocol for Wireless Sensor Networks [van Hoesel 04] ).  
 <a href="a00126.html#details">More...</a></p>

<p><code>#include &lt;<a class="el" href="a00432_source.html">LMacLayer.h</a>&gt;</code></p>
<div class="dynheader">
Inheritance diagram for LMacLayer:</div>
<div class="dyncontent">
<div class="center"><img src="a00826.png" border="0" usemap="#LMacLayer_inherit__map" alt="Inheritance graph"/></div>
<center><span class="legend">[<a target="top" href="graph_legend.html">legend</a>]</span></center></div>
<div class="dynheader">
Collaboration diagram for LMacLayer:</div>
<div class="dyncontent">
<div class="center"><img src="a00827.png" border="0" usemap="#LMacLayer_coll__map" alt="Collaboration graph"/></div>
<center><span class="legend">[<a target="top" href="graph_legend.html">legend</a>]</span></center></div>

<p><a href="a00828.html">List of all members.</a></p>
<table class="memberdecls">
<tr><td colspan="2"><h2><a name="pub-methods"></a>
Public Member Functions</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="ae1dd0f3eaa94d6860542146339b10e42"></a><!-- doxytag: member="LMacLayer::~LMacLayer" ref="ae1dd0f3eaa94d6860542146339b10e42" args="()" -->
virtual&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#ae1dd0f3eaa94d6860542146339b10e42">~LMacLayer</a> ()</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Clean up messges. <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a2100ca7de5c5eedb3e7f0e11ca9fca14">initialize</a> (int)</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Initialization of the module and some variables.  <a href="#a2100ca7de5c5eedb3e7f0e11ca9fca14"></a><br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="ab932e34db67404a0633a2b90e01f4d0f"></a><!-- doxytag: member="LMacLayer::finish" ref="ab932e34db67404a0633a2b90e01f4d0f" args="()" -->
virtual void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#ab932e34db67404a0633a2b90e01f4d0f">finish</a> ()</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Delete all dynamically allocated objects of the module. <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a0a7619fa37aadbcc587c735cc5cbcf99">handleLowerMsg</a> (cMessage *)</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Handle messages from lower layer.  <a href="#a0a7619fa37aadbcc587c735cc5cbcf99"></a><br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a7de5ee6f865e3ae8e7a9841291d36a37">handleUpperMsg</a> (cMessage *)</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Handle messages from upper layer.  <a href="#a7de5ee6f865e3ae8e7a9841291d36a37"></a><br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a2397193676d296cc6757b31eef203bb3">handleSelfMsg</a> (cMessage *)</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Handle self messages such as timers.  <a href="#a2397193676d296cc6757b31eef203bb3"></a><br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a28c3d64e3643d3b72d153036797fffa0">handleLowerControl</a> (cMessage *msg)</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Handle control messages from lower layer.  <a href="#a28c3d64e3643d3b72d153036797fffa0"></a><br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual macpkt_ptr_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a85b92d5d2292228cb408f4e72fca7caf">encapsMsg</a> (cPacket *)</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Encapsulate the NetwPkt into an MacPkt.  <a href="#a85b92d5d2292228cb408f4e72fca7caf"></a><br/></td></tr>
<tr><td colspan="2"><h2><a name="pro-types"></a>
Protected Types</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">enum &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a201a85e1ae744df103db6773739690ff">States</a> { <br/>
&#160;&#160;<b>INIT</b>, 
<b>SLEEP</b>, 
<b>CCA</b>, 
<b>WAIT_CONTROL</b>, 
<br/>
&#160;&#160;<b>WAIT_DATA</b>, 
<b>SEND_CONTROL</b>, 
<b>SEND_DATA</b>
<br/>
 }</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">MAC states.  <a href="a00126.html#a201a85e1ae744df103db6773739690ff">More...</a><br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">enum &#160;</td><td class="memItemRight" valign="bottom"><b>TYPES</b> { <br/>
&#160;&#160;<b>LMAC_CONTROL</b> =  167, 
<b>LMAC_TIMEOUT</b> =  168, 
<b>LMAC_WAKEUP</b> =  169, 
<b>LMAC_SEND_DATA</b> =  170, 
<br/>
&#160;&#160;<b>LMAC_SETUP_PHASE_END</b> =  171, 
<b>LMAC_CHECK_CHANNEL</b> =  172, 
<b>LMAC_SOMEBODY</b> =  173, 
<b>LMAC_DATA</b> =  174, 
<br/>
&#160;&#160;<b>LMAC_START_LMAC</b> =  175, 
<b>LMAC_SEND_CONTROL</b> =  176
<br/>
 }</td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="aaa9c0584a77cf47d352e0b2d0c3ed7ed"></a><!-- doxytag: member="LMacLayer::MacQueue" ref="aaa9c0584a77cf47d352e0b2d0c3ed7ed" args="" -->
typedef std::list&lt; LMacPkt * &gt;&#160;</td><td class="memItemRight" valign="bottom"><b>MacQueue</b></td></tr>
<tr><td colspan="2"><h2><a name="pro-methods"></a>
Protected Member Functions</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a16e8e67123131834e7a3b4371c518203">findNewSlot</a> ()</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">find a new slot  <a href="#a16e8e67123131834e7a3b4371c518203"></a><br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a14db63191f60dc3f3b906ce02eec6628"></a><!-- doxytag: member="LMacLayer::attachSignal" ref="a14db63191f60dc3f3b906ce02eec6628" args="(macpkt_ptr_t macPkt)" -->
void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a14db63191f60dc3f3b906ce02eec6628">attachSignal</a> (macpkt_ptr_t macPkt)</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Internal function to attach a signal to the packet. <br/></td></tr>
<tr><td colspan="2"><h2><a name="pro-attribs"></a>
Protected Attributes</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a6c97a461db5cd91f55d3d924b2a184dd"></a><!-- doxytag: member="LMacLayer::SETUP_PHASE" ref="a6c97a461db5cd91f55d3d924b2a184dd" args="" -->
bool&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a6c97a461db5cd91f55d3d924b2a184dd">SETUP_PHASE</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">the setup phase is the beginning of the simulation, where only control packets at very small slot durations are exchanged. <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="aae582a227411bd299295dec277ab7ad0"></a><!-- doxytag: member="LMacLayer::slotChange" ref="aae582a227411bd299295dec277ab7ad0" args="" -->
cOutVector *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#aae582a227411bd299295dec277ab7ad0">slotChange</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">indicate how often the node needs to change its slot because of collisions <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a48ad85f4aaba675b8f5287ad2bdd6007"></a><!-- doxytag: member="LMacLayer::macState" ref="a48ad85f4aaba675b8f5287ad2bdd6007" args="" -->
<a class="el" href="a00126.html#a201a85e1ae744df103db6773739690ff">States</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007">macState</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">keep track of MAC state <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a87f91fbb028f4b50115f3831f135da99"></a><!-- doxytag: member="LMacLayer::radioState" ref="a87f91fbb028f4b50115f3831f135da99" args="" -->
<a class="el" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7">MiximRadio::RadioState</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a87f91fbb028f4b50115f3831f135da99">radioState</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Current state of active channel (radio), set using radio, updated via BB. <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="aadcf84520c3dec4d76236cac8fdff135"></a><!-- doxytag: member="LMacLayer::slotDuration" ref="aadcf84520c3dec4d76236cac8fdff135" args="" -->
double&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#aadcf84520c3dec4d76236cac8fdff135">slotDuration</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Duration of a slot. <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a3bcd0ad076a7868e7d8ae0e591d46fec"></a><!-- doxytag: member="LMacLayer::controlDuration" ref="a3bcd0ad076a7868e7d8ae0e591d46fec" args="" -->
double&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a3bcd0ad076a7868e7d8ae0e591d46fec">controlDuration</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Duration of teh control time in each slot. <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a86d7beca6b3521cb80b44cd8c239b48d"></a><!-- doxytag: member="LMacLayer::mySlot" ref="a86d7beca6b3521cb80b44cd8c239b48d" args="" -->
int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d">mySlot</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">my slot ID <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a5182c67d55642bb0789bed7222a9e36c"></a><!-- doxytag: member="LMacLayer::numSlots" ref="a5182c67d55642bb0789bed7222a9e36c" args="" -->
int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c">numSlots</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">how many slots are there <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="aee8d9b764a36dd71fc189cbf6ef56d6e"></a><!-- doxytag: member="LMacLayer::currSlot" ref="aee8d9b764a36dd71fc189cbf6ef56d6e" args="" -->
int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#aee8d9b764a36dd71fc189cbf6ef56d6e">currSlot</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">The current slot of the simulation. <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a26e2b33eae9cef9a2b578173deba9a05"></a><!-- doxytag: member="LMacLayer::occSlotsDirect" ref="a26e2b33eae9cef9a2b578173deba9a05" args="[64]" -->
<a class="el" href="a00117.html#aea56b60dcb5ae8c2bde465271daf7210">LAddress::L2Type</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a26e2b33eae9cef9a2b578173deba9a05">occSlotsDirect</a> [64]</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Occupied slots from nodes, from which I hear directly. <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a28d9565b4b0a059ce0826e2610afb912"></a><!-- doxytag: member="LMacLayer::occSlotsAway" ref="a28d9565b4b0a059ce0826e2610afb912" args="[64]" -->
<a class="el" href="a00117.html#aea56b60dcb5ae8c2bde465271daf7210">LAddress::L2Type</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912">occSlotsAway</a> [64]</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Occupied slots of two-hop neighbors. <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="aa5508d9b952be702c8a4d33d63dc517c"></a><!-- doxytag: member="LMacLayer::reservedMobileSlots" ref="aa5508d9b952be702c8a4d33d63dc517c" args="" -->
int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#aa5508d9b952be702c8a4d33d63dc517c">reservedMobileSlots</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">The first couple of slots are reserved for nodes with special needs to avoid changing slots for them (mobile nodes) <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a4d4d0764f3a0b35fce09f71fa334ede5"></a><!-- doxytag: member="LMacLayer::macQueue" ref="a4d4d0764f3a0b35fce09f71fa334ede5" args="" -->
MacQueue&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a4d4d0764f3a0b35fce09f71fa334ede5">macQueue</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">A queue to store packets from upper layer in case another packet is still waiting for transmission.. <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="aa13209c2a38ad4cb43f4c73a3283e55c"></a><!-- doxytag: member="LMacLayer::queueLength" ref="aa13209c2a38ad4cb43f4c73a3283e55c" args="" -->
unsigned&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#aa13209c2a38ad4cb43f4c73a3283e55c">queueLength</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">length of the queue <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a5f29d36a7b775d2e5759171385408099"></a><!-- doxytag: member="LMacLayer::wakeup" ref="a5f29d36a7b775d2e5759171385408099" args="" -->
cMessage *&#160;</td><td class="memItemRight" valign="bottom"><b>wakeup</b></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="ab145d55474f361269726b49274c60ebb"></a><!-- doxytag: member="LMacLayer::timeout" ref="ab145d55474f361269726b49274c60ebb" args="" -->
cMessage *&#160;</td><td class="memItemRight" valign="bottom"><b>timeout</b></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a3e185546d0082216505c25f6d5d589ef"></a><!-- doxytag: member="LMacLayer::sendData" ref="a3e185546d0082216505c25f6d5d589ef" args="" -->
cMessage *&#160;</td><td class="memItemRight" valign="bottom"><b>sendData</b></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a1f8dff57ffe7b1168b88b113fe0fa5ff"></a><!-- doxytag: member="LMacLayer::initChecker" ref="a1f8dff57ffe7b1168b88b113fe0fa5ff" args="" -->
cMessage *&#160;</td><td class="memItemRight" valign="bottom"><b>initChecker</b></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a9af231c98754f8d593aed11808e46614"></a><!-- doxytag: member="LMacLayer::checkChannel" ref="a9af231c98754f8d593aed11808e46614" args="" -->
cMessage *&#160;</td><td class="memItemRight" valign="bottom"><b>checkChannel</b></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a53ddfdcb1ddd6f9eac3b4f614fc89d4d"></a><!-- doxytag: member="LMacLayer::start_lmac" ref="a53ddfdcb1ddd6f9eac3b4f614fc89d4d" args="" -->
cMessage *&#160;</td><td class="memItemRight" valign="bottom"><b>start_lmac</b></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="aad1a5ee678f294b134ee56f0b96a4fa1"></a><!-- doxytag: member="LMacLayer::send_control" ref="aad1a5ee678f294b134ee56f0b96a4fa1" args="" -->
cMessage *&#160;</td><td class="memItemRight" valign="bottom"><b>send_control</b></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="ab797f8cfd8e9b42c7ca72ed33c4af8d8"></a><!-- doxytag: member="LMacLayer::bitrate" ref="ab797f8cfd8e9b42c7ca72ed33c4af8d8" args="" -->
double&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#ab797f8cfd8e9b42c7ca72ed33c4af8d8">bitrate</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">the bit rate at which we transmit <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a5631de1b0bad61ff8c6366040cc4681c"></a><!-- doxytag: member="LMacLayer::droppedPacket" ref="a5631de1b0bad61ff8c6366040cc4681c" args="" -->
<a class="el" href="a00088.html">DroppedPacket</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a5631de1b0bad61ff8c6366040cc4681c">droppedPacket</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Inspect reasons for dropped packets. <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a64482bc131ae8b5598fbf97db66f4552"></a><!-- doxytag: member="LMacLayer::nicId" ref="a64482bc131ae8b5598fbf97db66f4552" args="" -->
int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a64482bc131ae8b5598fbf97db66f4552">nicId</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">publish dropped packets nic wide <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="abe6c59398a7e27aa3e421e65690ed6e8"></a><!-- doxytag: member="LMacLayer::txPower" ref="abe6c59398a7e27aa3e421e65690ed6e8" args="" -->
double&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#abe6c59398a7e27aa3e421e65690ed6e8">txPower</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Transmission power of the node. <br/></td></tr>
<tr><td colspan="2"><h2><a name="pro-static-attribs"></a>
Static Protected Attributes</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a04b4749559221c7d8f886e01ecc123e6"></a><!-- doxytag: member="LMacLayer::LMAC_NO_RECEIVER" ref="a04b4749559221c7d8f886e01ecc123e6" args="" -->
static const <a class="el" href="a00117.html#aea56b60dcb5ae8c2bde465271daf7210">LAddress::L2Type</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#a04b4749559221c7d8f886e01ecc123e6">LMAC_NO_RECEIVER</a></td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">dummy receiver address to indicate no pending packets in the control packet <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="a83dd6174b208019b6422349c1ded0e87"></a><!-- doxytag: member="LMacLayer::LMAC_FREE_SLOT" ref="a83dd6174b208019b6422349c1ded0e87" args="" -->
static const <a class="el" href="a00117.html#aea56b60dcb5ae8c2bde465271daf7210">LAddress::L2Type</a>&#160;</td><td class="memItemRight" valign="bottom"><b>LMAC_FREE_SLOT</b></td></tr>
<tr><td colspan="2"><h2><a name="pri-methods"></a>
Private Member Functions</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="ad94b652c104d0d286c3035d0febdb61f"></a><!-- doxytag: member="LMacLayer::LMacLayer" ref="ad94b652c104d0d286c3035d0febdb61f" args="(const LMacLayer &amp;)" -->
&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#ad94b652c104d0d286c3035d0febdb61f">LMacLayer</a> (const <a class="el" href="a00126.html">LMacLayer</a> &amp;)</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Copy constructor is not allowed. <br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="abdc0b711b4cc157a0da08ccfbbd4060c"></a><!-- doxytag: member="LMacLayer::operator=" ref="abdc0b711b4cc157a0da08ccfbbd4060c" args="(const LMacLayer &amp;)" -->
<a class="el" href="a00126.html">LMacLayer</a> &amp;&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00126.html#abdc0b711b4cc157a0da08ccfbbd4060c">operator=</a> (const <a class="el" href="a00126.html">LMacLayer</a> &amp;)</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Assignment operator is not allowed. <br/></td></tr>
</table>
<hr/><a name="details" id="details"></a><h2>Detailed Description</h2>
<div class="textblock"><p>Implementation of L-MAC (Lightweight Medium Access Protocol for Wireless Sensor Networks [van Hoesel 04] ). </p>
<p>Each node has its own unique timeslot. Nodes can use their timeslots to transfer data without having to contend for the medium or having to deal with energy wasting collisions or retransmissions.</p>
<p>During the first 5 full frames nodes will be waking up every controlDuration to setup the network first by assigning a different timeslot to each node. Normal packets will be queued, but will be send only after the setup phase.</p>
<p>During its timeslot a node wakes up, checks the channel for a short random period (CCA) to check for possible collision in the slot and, if the channel is free, sends a control packet. If there is a collision it tries to change its timeslot to an empty one. After the transmission of the control packet it checks its packet queue and if its non-empty it sends a data packet.</p>
<p>During a foreign timeslot a node wakes up, checks the channel for 2*controlDuration period for an incoming control packet and if there in nothing it goes back to sleep and conserves energy for the rest of the timeslot. If it receives a control packet addressed for itself it stays awake for the rest of the timeslot to receive the incoming data packet.</p>
<p>The finite state machine of the protocol is given in the below figure:</p>
<div class="image">
<img src="LMACFSM.jpg" alt="LMACFSM.jpg"/>
<div class="caption">
State chart for LMAC layer</div></div>
<p> A paper describing the protocol is:</p>
<p>L. van Hoesel and P. Havinga. A lightweight medium access protocol (LMAC) for wireless sensor networks. In Proceedings of the 3rd International Symposium on Information Processing in Sensor Networks (IPSN), pages 55-60, Berkeley, CA, February 2004. April. </p>
</div><hr/><h2>Member Enumeration Documentation</h2>
<a class="anchor" id="a201a85e1ae744df103db6773739690ff"></a><!-- doxytag: member="LMacLayer::States" ref="a201a85e1ae744df103db6773739690ff" args="" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">enum <a class="el" href="a00126.html#a201a85e1ae744df103db6773739690ff">LMacLayer::States</a><code> [protected]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>MAC states. </p>
<p>The MAC states help to keep track what the MAC is actually trying to do -- this is esp. useful when radio switching takes some time. SLEEP -- the node sleeps but accepts packets from the network layer RX -- MAC accepts packets from PHY layer TX -- MAC transmits a packet CCA -- Clear Channel Assessment - MAC checks whether medium is busy </p>
<div class="fragment"><pre class="fragment">        {
            INIT, SLEEP, CCA, WAIT_CONTROL, WAIT_DATA, SEND_CONTROL, SEND_DATA
        };
</pre></div>
</div>
</div>
<hr/><h2>Member Function Documentation</h2>
<a class="anchor" id="a85b92d5d2292228cb408f4e72fca7caf"></a><!-- doxytag: member="LMacLayer::encapsMsg" ref="a85b92d5d2292228cb408f4e72fca7caf" args="(cPacket *)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">LMacLayer::macpkt_ptr_t LMacLayer::encapsMsg </td>
          <td>(</td>
          <td class="paramtype">cPacket *&#160;</td>
          <td class="paramname"><em>msg</em></td><td>)</td>
          <td><code> [virtual]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>Encapsulate the NetwPkt into an MacPkt. </p>
<p>Encapsulates the received network-layer packet into a MacPkt and set all needed header fields. </p>

<p>Reimplemented from <a class="el" href="a00026.html#a0e1c8dad62e40f43a43c7b8a0cb71501">BaseMacLayer</a>.</p>

<p>References <a class="el" href="a00026.html#a48135ffa21044a71d92149d5301f9deb">BaseMacLayer::getUpperDestinationFromControlInfo()</a>, <a class="el" href="a00026.html#a26ddb186455b73c8dc8b5698296804d2">BaseMacLayer::headerLength</a>, and <a class="el" href="a00026.html#a8a26df9da07801d8ccb530530ef57ee2">BaseMacLayer::myMacAddr</a>.</p>

<p>Referenced by <a class="el" href="a00126.html#a7de5ee6f865e3ae8e7a9841291d36a37">handleUpperMsg()</a>.</p>
<div class="fragment"><pre class="fragment">{

    LMacPkt *pkt = <span class="keyword">new</span> LMacPkt(msg-&gt;getName(), msg-&gt;getKind());
    pkt-&gt;setBitLength(<a class="code" href="a00026.html#a26ddb186455b73c8dc8b5698296804d2" title="Pointer to the arp module.">headerLength</a>);

    <span class="comment">// copy dest address from the Control Info attached to the network</span>
    <span class="comment">// mesage by the network layer</span>
    cObject *<span class="keyword">const</span> cInfo = msg-&gt;removeControlInfo();

    debugEV &lt;&lt; <span class="stringliteral">&quot;CInfo removed, mac addr=&quot;</span> &lt;&lt; <a class="code" href="a00026.html#a48135ffa21044a71d92149d5301f9deb" title="Extracts the MAC address from the &quot;control info&quot; structure (object).">getUpperDestinationFromControlInfo</a>(cInfo) &lt;&lt; endl;
    pkt-&gt;setDestAddr(<a class="code" href="a00026.html#a48135ffa21044a71d92149d5301f9deb" title="Extracts the MAC address from the &quot;control info&quot; structure (object).">getUpperDestinationFromControlInfo</a>(cInfo));

    <span class="comment">//delete the control info</span>
    <span class="keyword">delete</span> cInfo;

    <span class="comment">//set the src address to own mac address (nic module getId())</span>
    pkt-&gt;setSrcAddr(<a class="code" href="a00026.html#a8a26df9da07801d8ccb530530ef57ee2" title="MAC address.">myMacAddr</a>);

    <span class="comment">//encapsulate the network packet</span>
    pkt-&gt;encapsulate(check_and_cast&lt;cPacket *&gt;(msg));
    debugEV &lt;&lt;<span class="stringliteral">&quot;pkt encapsulated\n&quot;</span>;

    <span class="keywordflow">return</span> pkt;

}
</pre></div>
</div>
</div>
<a class="anchor" id="a16e8e67123131834e7a3b4371c518203"></a><!-- doxytag: member="LMacLayer::findNewSlot" ref="a16e8e67123131834e7a3b4371c518203" args="()" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void LMacLayer::findNewSlot </td>
          <td>(</td>
          <td class="paramname"></td><td>)</td>
          <td><code> [protected]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>find a new slot </p>
<p>Try to find a new slot after collision. If not possible, set own slot to -1 (not able to send anything) </p>

<p>References <a class="el" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d">mySlot</a>, <a class="el" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c">numSlots</a>, <a class="el" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912">occSlotsAway</a>, <a class="el" href="a00126.html#aa5508d9b952be702c8a4d33d63dc517c">reservedMobileSlots</a>, and <a class="el" href="a00126.html#aae582a227411bd299295dec277ab7ad0">slotChange</a>.</p>

<p>Referenced by <a class="el" href="a00126.html#a2397193676d296cc6757b31eef203bb3">handleSelfMsg()</a>.</p>
<div class="fragment"><pre class="fragment">{
  <span class="comment">// pick a random slot at the beginning and schedule the next wakeup</span>
  <span class="comment">// free the old one first</span>
  <span class="keywordtype">int</span> counter = 0;

  <a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> = intrand((<a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a> - <a class="code" href="a00126.html#aa5508d9b952be702c8a4d33d63dc517c" title="The first couple of slots are reserved for nodes with special needs to avoid changing slots for them ...">reservedMobileSlots</a>));
  <span class="keywordflow">while</span> ((<a class="code" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912" title="Occupied slots of two-hop neighbors.">occSlotsAway</a>[<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a>] != LMAC_FREE_SLOT) &amp;&amp; (counter &lt; (<a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a> - <a class="code" href="a00126.html#aa5508d9b952be702c8a4d33d63dc517c" title="The first couple of slots are reserved for nodes with special needs to avoid changing slots for them ...">reservedMobileSlots</a>)))
  {
    counter++;
    <a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a>--;
    <span class="keywordflow">if</span> (<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> &lt; 0)
      <a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> = (<a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a> - <a class="code" href="a00126.html#aa5508d9b952be702c8a4d33d63dc517c" title="The first couple of slots are reserved for nodes with special needs to avoid changing slots for them ...">reservedMobileSlots</a>)-1;
  }
  <span class="keywordflow">if</span> (<a class="code" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912" title="Occupied slots of two-hop neighbors.">occSlotsAway</a>[<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a>] != LMAC_FREE_SLOT)
  {
    EV &lt;&lt; <span class="stringliteral">&quot;ERROR: I cannot find a free slot. Cannot send data.\n&quot;</span>;
    <a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> = -1;
  }
  <span class="keywordflow">else</span>
  {
    EV &lt;&lt; <span class="stringliteral">&quot;ERROR: My new slot is : &quot;</span> &lt;&lt; <a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> &lt;&lt; endl;
  }
  EV &lt;&lt; <span class="stringliteral">&quot;ERROR: I needed to find new slot\n&quot;</span>;
  <a class="code" href="a00126.html#aae582a227411bd299295dec277ab7ad0" title="indicate how often the node needs to change its slot because of collisions">slotChange</a>-&gt;recordWithTimestamp(simTime(), <a class="code" href="a00093.html" title="Provides method templates to find omnet modules.">FindModule&lt;&gt;::findHost</a>(<span class="keyword">this</span>)-&gt;getId()-4);
}
</pre></div>
</div>
</div>
<a class="anchor" id="a28c3d64e3643d3b72d153036797fffa0"></a><!-- doxytag: member="LMacLayer::handleLowerControl" ref="a28c3d64e3643d3b72d153036797fffa0" args="(cMessage *msg)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void LMacLayer::handleLowerControl </td>
          <td>(</td>
          <td class="paramtype">cMessage *&#160;</td>
          <td class="paramname"><em>msg</em></td><td>)</td>
          <td><code> [virtual]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>Handle control messages from lower layer. </p>
<p>Handle transmission over messages: send the data packet or don;t do anyhting. </p>

<p>Reimplemented from <a class="el" href="a00026.html#a796d456d593dfab3188b37e506dd76a8">BaseMacLayer</a>.</p>

<p>References <a class="el" href="a00134.html#a00dd43576b4da205117d53bd3baadc71">MacToPhyInterface::getRadioState()</a>, <a class="el" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007">macState</a>, <a class="el" href="a00026.html#aa7b1e6bdb734765a9f4eff7d486dfbea">BaseMacLayer::phy</a>, <a class="el" href="a00134.html#a36bc50a86bf1cc0f9efd374613d61a52a876c7366bd2bcb675b41e60007831d06">MacToPhyInterface::RADIO_SWITCHING_OVER</a>, <a class="el" href="a00134.html#ad25abbad9aa16bdbf24bf1dd582b1c6f">MacToPhyInterface::setRadioState()</a>, <a class="el" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7afd684ca0a1c84f153dec831272ccde42">MiximRadio::SLEEP</a>, <a class="el" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7a4f6c3dfae0f5fccb76b1d42f6ccc785d">MiximRadio::TX</a>, and <a class="el" href="a00026.html#a49e0e548a6c20d002bd67afa7b76c483a5e5e0e55f7cc75b0d8bad06a212371f1">BaseMacLayer::TX_OVER</a>.</p>
<div class="fragment"><pre class="fragment">{
  <span class="keywordflow">if</span>(msg-&gt;getKind() == <a class="code" href="a00026.html#a49e0e548a6c20d002bd67afa7b76c483a5e5e0e55f7cc75b0d8bad06a212371f1">MacToPhyInterface::TX_OVER</a>)
  {
    <span class="comment">// if data is scheduled for transfer, don;t do anything.</span>
    <span class="keywordflow">if</span> (sendData-&gt;isScheduled())
    {
      debugEV &lt;&lt; <span class="stringliteral">&quot; transmission of control packet over. data transfer will start soon.&quot;</span> &lt;&lt; endl;
      <span class="keyword">delete</span> msg;
      <span class="keywordflow">return</span>;
    }
    <span class="keywordflow">else</span>
    {
      debugEV &lt;&lt; <span class="stringliteral">&quot; transmission over. nothing else is scheduled, get back to sleep.&quot;</span> &lt;&lt; endl;
      <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> = SLEEP;
      debugEV &lt;&lt; <span class="stringliteral">&quot;Old state: ?, New state: SLEEP&quot;</span> &lt;&lt; endl;
      <a class="code" href="a00026.html#aa7b1e6bdb734765a9f4eff7d486dfbea" title="Handler to the physical layer.">phy</a>-&gt;<a class="code" href="a00134.html#ad25abbad9aa16bdbf24bf1dd582b1c6f" title="Tells the BasePhyLayer to switch to the specified radio state.">setRadioState</a>(<a class="code" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7afd684ca0a1c84f153dec831272ccde42" title="sleeping">MiximRadio::SLEEP</a>);
      <span class="keywordflow">if</span> (timeout-&gt;isScheduled())
        cancelEvent(timeout);
    }
  }
  <span class="keywordflow">else</span> <span class="keywordflow">if</span>(msg-&gt;getKind() == <a class="code" href="a00134.html#a36bc50a86bf1cc0f9efd374613d61a52a876c7366bd2bcb675b41e60007831d06" title="Indicates the end of a radio switch.">MacToPhyInterface::RADIO_SWITCHING_OVER</a>)
  {
      <span class="comment">// we just switched to TX after CCA, so simply send the first sendPremable self message</span>
      <span class="keywordflow">if</span> ((<a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> == SEND_CONTROL) &amp;&amp; (<a class="code" href="a00026.html#aa7b1e6bdb734765a9f4eff7d486dfbea" title="Handler to the physical layer.">phy</a>-&gt;<a class="code" href="a00134.html#a00dd43576b4da205117d53bd3baadc71" title="Returns the current state the radio is in. See RadioState for possible values.">getRadioState</a>() == <a class="code" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7a4f6c3dfae0f5fccb76b1d42f6ccc785d" title="transmitting state">MiximRadio::TX</a>))
      {
        scheduleAt(simTime(), send_control);
      }
  }
  <span class="keywordflow">else</span> {
    EV &lt;&lt; <span class="stringliteral">&quot;control message with wrong kind -- deleting\n&quot;</span>;
  }
  <span class="keyword">delete</span> msg;
}
</pre></div>
</div>
</div>
<a class="anchor" id="a0a7619fa37aadbcc587c735cc5cbcf99"></a><!-- doxytag: member="LMacLayer::handleLowerMsg" ref="a0a7619fa37aadbcc587c735cc5cbcf99" args="(cMessage *)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void LMacLayer::handleLowerMsg </td>
          <td>(</td>
          <td class="paramtype">cMessage *&#160;</td>
          <td class="paramname"><em>msg</em></td><td>)</td>
          <td><code> [virtual]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>Handle messages from lower layer. </p>
<p>Handle LMAC control packets and data packets. Recognize collisions, change own slot if necessary and remember who is using which slot. </p>

<p>Reimplemented from <a class="el" href="a00026.html#a413375cec02b990a521b62086a02264c">BaseMacLayer</a>.</p>

<p>References <a class="el" href="a00126.html#a2397193676d296cc6757b31eef203bb3">handleSelfMsg()</a>.</p>
<div class="fragment"><pre class="fragment">{
  <span class="comment">// simply pass the massage as self message, to be processed by the FSM.</span>
  <a class="code" href="a00126.html#a2397193676d296cc6757b31eef203bb3" title="Handle self messages such as timers.">handleSelfMsg</a>(msg);
}
</pre></div>
</div>
</div>
<a class="anchor" id="a2397193676d296cc6757b31eef203bb3"></a><!-- doxytag: member="LMacLayer::handleSelfMsg" ref="a2397193676d296cc6757b31eef203bb3" args="(cMessage *)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void LMacLayer::handleSelfMsg </td>
          <td>(</td>
          <td class="paramtype">cMessage *&#160;</td>
          <td class="paramname"><em>msg</em></td><td>)</td>
          <td><code> [virtual]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>Handle self messages such as timers. </p>
<p>Handle self messages: LMAC_SETUP_PHASE_END: end of setup phase. Change slot duration to normal and start sending data packets. The slots of the nodes should be stable now. LMAC_SEND_DATA: send the data packet. LMAC_CHECK_CHANNEL: check the channel in own slot. If busy, change the slot. If not, send a control packet. LMAC_WAKEUP: wake up the node and either check the channel before sending a control packet or wait for control packets. LMAC_TIMEOUT: go back to sleep after nothing happened. </p>

<p>Reimplemented from <a class="el" href="a00026.html#add5277c1a60dab10db40c8d9641e48c4">BaseMacLayer</a>.</p>

<p>References <a class="el" href="a00126.html#a14db63191f60dc3f3b906ce02eec6628">attachSignal()</a>, <a class="el" href="a00126.html#a3bcd0ad076a7868e7d8ae0e591d46fec">controlDuration</a>, <a class="el" href="a00126.html#aee8d9b764a36dd71fc189cbf6ef56d6e">currSlot</a>, <a class="el" href="a00026.html#a998b4c4c56074a354b50f8c4fc5d6d4c">BaseMacLayer::decapsMsg()</a>, <a class="el" href="a00027.html#aabf0226159b22b46e70031fd3bb3b1b2">BaseModule::findHost()</a>, <a class="el" href="a00126.html#a16e8e67123131834e7a3b4371c518203">findNewSlot()</a>, <a class="el" href="a00026.html#a26ddb186455b73c8dc8b5698296804d2">BaseMacLayer::headerLength</a>, <a class="el" href="a00117.html#a3890f1c7314c5695e32252468e6bd5a5">LAddress::isL2Broadcast()</a>, <a class="el" href="a00126.html#a04b4749559221c7d8f886e01ecc123e6">LMAC_NO_RECEIVER</a>, <a class="el" href="a00126.html#a4d4d0764f3a0b35fce09f71fa334ede5">macQueue</a>, <a class="el" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007">macState</a>, <a class="el" href="a00026.html#a8a26df9da07801d8ccb530530ef57ee2">BaseMacLayer::myMacAddr</a>, <a class="el" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d">mySlot</a>, <a class="el" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c">numSlots</a>, <a class="el" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912">occSlotsAway</a>, <a class="el" href="a00126.html#a26e2b33eae9cef9a2b578173deba9a05">occSlotsDirect</a>, <a class="el" href="a00026.html#aa7b1e6bdb734765a9f4eff7d486dfbea">BaseMacLayer::phy</a>, <a class="el" href="a00126.html#aa5508d9b952be702c8a4d33d63dc517c">reservedMobileSlots</a>, <a class="el" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7a2c1aea88fe8363141c138da817461953">MiximRadio::RX</a>, <a class="el" href="a00025.html#a1873d6e3fc6d92cf8482b39f1c5ac6c8">BaseLayer::sendDown()</a>, <a class="el" href="a00025.html#adbf061566606041db811b519f0400ed5">BaseLayer::sendUp()</a>, <a class="el" href="a00134.html#ad25abbad9aa16bdbf24bf1dd582b1c6f">MacToPhyInterface::setRadioState()</a>, <a class="el" href="a00126.html#a6c97a461db5cd91f55d3d924b2a184dd">SETUP_PHASE</a>, <a class="el" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7afd684ca0a1c84f153dec831272ccde42">MiximRadio::SLEEP</a>, <a class="el" href="a00126.html#aadcf84520c3dec4d76236cac8fdff135">slotDuration</a>, and <a class="el" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7a4f6c3dfae0f5fccb76b1d42f6ccc785d">MiximRadio::TX</a>.</p>

<p>Referenced by <a class="el" href="a00126.html#a0a7619fa37aadbcc587c735cc5cbcf99">handleLowerMsg()</a>.</p>
<div class="fragment"><pre class="fragment">{
  <span class="keywordflow">switch</span> (<a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a>)
  {
  <span class="keywordflow">case</span> INIT:
    <span class="keywordflow">if</span> (msg-&gt;getKind() == LMAC_START_LMAC)
    {
      <span class="comment">// the first 5 full slots we will be waking up every controlDuration to setup the network first</span>
      <span class="comment">// normal packets will be queued, but will be send only after the setup phase</span>
      scheduleAt(<a class="code" href="a00126.html#aadcf84520c3dec4d76236cac8fdff135" title="Duration of a slot.">slotDuration</a>*5*<a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a>, initChecker);
      coreEV &lt;&lt; <span class="stringliteral">&quot;Startup time =&quot;</span> &lt;&lt; <a class="code" href="a00126.html#aadcf84520c3dec4d76236cac8fdff135" title="Duration of a slot.">slotDuration</a>*5*<a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a> &lt;&lt; endl;

      debugEV &lt;&lt; <span class="stringliteral">&quot;Scheduling the first wakeup at : &quot;</span> &lt;&lt; <a class="code" href="a00126.html#aadcf84520c3dec4d76236cac8fdff135" title="Duration of a slot.">slotDuration</a> &lt;&lt; endl;

      scheduleAt(<a class="code" href="a00126.html#aadcf84520c3dec4d76236cac8fdff135" title="Duration of a slot.">slotDuration</a>, wakeup);

      <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a>; i++)
      {
        <a class="code" href="a00126.html#a26e2b33eae9cef9a2b578173deba9a05" title="Occupied slots from nodes, from which I hear directly.">occSlotsDirect</a>[i] = LMAC_FREE_SLOT;
        <a class="code" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912" title="Occupied slots of two-hop neighbors.">occSlotsAway</a>[i]   = LMAC_FREE_SLOT;
      }

      <span class="keywordflow">if</span> (myId &gt;= <a class="code" href="a00126.html#aa5508d9b952be702c8a4d33d63dc517c" title="The first couple of slots are reserved for nodes with special needs to avoid changing slots for them ...">reservedMobileSlots</a>)
        <a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> = ((int) <a class="code" href="a00093.html" title="Provides method templates to find omnet modules.">FindModule&lt;&gt;::findHost</a>(<span class="keyword">this</span>)-&gt;getId() ) % (numSlots - <a class="code" href="a00126.html#aa5508d9b952be702c8a4d33d63dc517c" title="The first couple of slots are reserved for nodes with special needs to avoid changing slots for them ...">reservedMobileSlots</a>);
      <span class="keywordflow">else</span>
        <a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> = myId;
      <span class="comment">//occSlotsDirect[mySlot] = myMacAddr;</span>
      <span class="comment">//occSlotsAway[mySlot] = myMacAddr;</span>
      <a class="code" href="a00126.html#aee8d9b764a36dd71fc189cbf6ef56d6e" title="The current slot of the simulation.">currSlot</a> = 0;

      debugEV &lt;&lt; <span class="stringliteral">&quot;ID: &quot;</span> &lt;&lt; <a class="code" href="a00027.html#aabf0226159b22b46e70031fd3bb3b1b2" title="Function to get a pointer to the host module.">FindModule&lt;&gt;::findHost</a>(<span class="keyword">this</span>)-&gt;getId() &lt;&lt; <span class="stringliteral">&quot;. Picked random slot: &quot;</span> &lt;&lt; <a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> &lt;&lt; endl;

      <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a>=SLEEP;
      debugEV &lt;&lt; <span class="stringliteral">&quot;Old state: INIT, New state: SLEEP&quot;</span> &lt;&lt; endl;
      <a class="code" href="a00126.html#a6c97a461db5cd91f55d3d924b2a184dd" title="the setup phase is the beginning of the simulation, where only control packets at very small slot dur...">SETUP_PHASE</a> = <span class="keyword">true</span>;
    }
    <span class="keywordflow">else</span> {
      EV &lt;&lt; <span class="stringliteral">&quot;Unknown packet&quot;</span> &lt;&lt; msg-&gt;getKind() &lt;&lt;  <span class="stringliteral">&quot;in state&quot;</span> &lt;&lt; <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> &lt;&lt; endl;
    }
    <span class="keywordflow">break</span>;

  <span class="keywordflow">case</span> SLEEP:
    <span class="keywordflow">if</span>(msg-&gt;getKind() == LMAC_WAKEUP)
    {
      <a class="code" href="a00126.html#aee8d9b764a36dd71fc189cbf6ef56d6e" title="The current slot of the simulation.">currSlot</a>++;
      <a class="code" href="a00126.html#aee8d9b764a36dd71fc189cbf6ef56d6e" title="The current slot of the simulation.">currSlot</a> %= <a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a>;
      debugEV &lt;&lt; <span class="stringliteral">&quot;New slot starting - No. &quot;</span> &lt;&lt; <a class="code" href="a00126.html#aee8d9b764a36dd71fc189cbf6ef56d6e" title="The current slot of the simulation.">currSlot</a> &lt;&lt; <span class="stringliteral">&quot;, my slot is &quot;</span> &lt;&lt; <a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> &lt;&lt; endl;

      <span class="keywordflow">if</span> (<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> == <a class="code" href="a00126.html#aee8d9b764a36dd71fc189cbf6ef56d6e" title="The current slot of the simulation.">currSlot</a>)
      {
        debugEV &lt;&lt; <span class="stringliteral">&quot;Waking up in my slot. Switch to RECV first to check the channel.\n&quot;</span>;
        <a class="code" href="a00026.html#aa7b1e6bdb734765a9f4eff7d486dfbea" title="Handler to the physical layer.">phy</a>-&gt;<a class="code" href="a00134.html#ad25abbad9aa16bdbf24bf1dd582b1c6f" title="Tells the BasePhyLayer to switch to the specified radio state.">setRadioState</a>(<a class="code" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7a2c1aea88fe8363141c138da817461953" title="receiving state">MiximRadio::RX</a>);
        <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> = CCA;
        debugEV &lt;&lt; <span class="stringliteral">&quot;Old state: SLEEP, New state: CCA&quot;</span> &lt;&lt; endl;

        <span class="keywordtype">double</span> small_delay = <a class="code" href="a00126.html#a3bcd0ad076a7868e7d8ae0e591d46fec" title="Duration of teh control time in each slot.">controlDuration</a>*dblrand();
        scheduleAt(simTime()+small_delay, checkChannel);
        debugEV &lt;&lt; <span class="stringliteral">&quot;Checking for channel for &quot;</span> &lt;&lt; small_delay &lt;&lt; <span class="stringliteral">&quot; time.\n&quot;</span>;
      }
      <span class="keywordflow">else</span>
      {
        debugEV &lt;&lt; <span class="stringliteral">&quot;Waking up in a foreign slot. Ready to receive control packet.\n&quot;</span>;
        <a class="code" href="a00026.html#aa7b1e6bdb734765a9f4eff7d486dfbea" title="Handler to the physical layer.">phy</a>-&gt;<a class="code" href="a00134.html#ad25abbad9aa16bdbf24bf1dd582b1c6f" title="Tells the BasePhyLayer to switch to the specified radio state.">setRadioState</a>(<a class="code" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7a2c1aea88fe8363141c138da817461953" title="receiving state">MiximRadio::RX</a>);
        <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> = WAIT_CONTROL;
        debugEV &lt;&lt; <span class="stringliteral">&quot;Old state: SLEEP, New state: WAIT_CONTROL&quot;</span> &lt;&lt; endl;
        <span class="keywordflow">if</span> (!<a class="code" href="a00126.html#a6c97a461db5cd91f55d3d924b2a184dd" title="the setup phase is the beginning of the simulation, where only control packets at very small slot dur...">SETUP_PHASE</a>) <span class="comment">//in setup phase do not sleep</span>
          scheduleAt(simTime()+2.f*<a class="code" href="a00126.html#a3bcd0ad076a7868e7d8ae0e591d46fec" title="Duration of teh control time in each slot.">controlDuration</a>, timeout);
      }
      <span class="keywordflow">if</span> (<a class="code" href="a00126.html#a6c97a461db5cd91f55d3d924b2a184dd" title="the setup phase is the beginning of the simulation, where only control packets at very small slot dur...">SETUP_PHASE</a>)
      {
        scheduleAt(simTime()+2.f*<a class="code" href="a00126.html#a3bcd0ad076a7868e7d8ae0e591d46fec" title="Duration of teh control time in each slot.">controlDuration</a>, wakeup);
        debugEV &lt;&lt; <span class="stringliteral">&quot;setup phase slot duration:&quot;</span> &lt;&lt; 2.f*<a class="code" href="a00126.html#a3bcd0ad076a7868e7d8ae0e591d46fec" title="Duration of teh control time in each slot.">controlDuration</a> &lt;&lt; <span class="stringliteral">&quot;while controlduration is&quot;</span> &lt;&lt; <a class="code" href="a00126.html#a3bcd0ad076a7868e7d8ae0e591d46fec" title="Duration of teh control time in each slot.">controlDuration</a> &lt;&lt; endl;
      }
      <span class="keywordflow">else</span>
        scheduleAt(simTime()+<a class="code" href="a00126.html#aadcf84520c3dec4d76236cac8fdff135" title="Duration of a slot.">slotDuration</a>, wakeup);
    }
    <span class="keywordflow">else</span> <span class="keywordflow">if</span>(msg-&gt;getKind() == LMAC_SETUP_PHASE_END)
    {
      debugEV &lt;&lt; <span class="stringliteral">&quot;Setup phase end. Start normal work at the next slot.\n&quot;</span>;
      <span class="keywordflow">if</span> (wakeup-&gt;isScheduled())
        cancelEvent(wakeup);

      scheduleAt(simTime()+<a class="code" href="a00126.html#aadcf84520c3dec4d76236cac8fdff135" title="Duration of a slot.">slotDuration</a>, wakeup);

      <a class="code" href="a00126.html#a6c97a461db5cd91f55d3d924b2a184dd" title="the setup phase is the beginning of the simulation, where only control packets at very small slot dur...">SETUP_PHASE</a> = <span class="keyword">false</span>;
    }
    <span class="keywordflow">else</span>
    {
      EV &lt;&lt; <span class="stringliteral">&quot;Unknown packet&quot;</span> &lt;&lt; msg-&gt;getKind() &lt;&lt;  <span class="stringliteral">&quot;in state&quot;</span> &lt;&lt; <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> &lt;&lt; endl;
    }
    <span class="keywordflow">break</span>;

  <span class="keywordflow">case</span> CCA:
    <span class="keywordflow">if</span>(msg-&gt;getKind() == LMAC_CHECK_CHANNEL)
    {
      <span class="comment">// if the channel is clear, get ready for sending the control packet</span>
      coreEV &lt;&lt; <span class="stringliteral">&quot;Channel is free, so let&#39;s prepare for sending.\n&quot;</span>;

      <a class="code" href="a00026.html#aa7b1e6bdb734765a9f4eff7d486dfbea" title="Handler to the physical layer.">phy</a>-&gt;<a class="code" href="a00134.html#ad25abbad9aa16bdbf24bf1dd582b1c6f" title="Tells the BasePhyLayer to switch to the specified radio state.">setRadioState</a>(<a class="code" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7a4f6c3dfae0f5fccb76b1d42f6ccc785d" title="transmitting state">MiximRadio::TX</a>);
      <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> = SEND_CONTROL;
      debugEV &lt;&lt; <span class="stringliteral">&quot;Old state: CCA, New state: SEND_CONTROL&quot;</span> &lt;&lt; endl;

    }
    <span class="keywordflow">else</span> <span class="keywordflow">if</span>(msg-&gt;getKind() == LMAC_CONTROL)
    {
      LMacPkt *<span class="keyword">const</span>          mac  = <span class="keyword">static_cast&lt;</span>LMacPkt *<span class="keyword">&gt;</span>(msg);
      <span class="keyword">const</span> <a class="code" href="a00117.html#aea56b60dcb5ae8c2bde465271daf7210" title="Type definition for a L2 (MAC) address.">LAddress::L2Type</a>&amp; dest = mac-&gt;getDestAddr();
      debugEV &lt;&lt; <span class="stringliteral">&quot; I have received a control packet from src &quot;</span> &lt;&lt; mac-&gt;getSrcAddr() &lt;&lt; <span class="stringliteral">&quot; and dest &quot;</span> &lt;&lt; dest &lt;&lt; <span class="stringliteral">&quot;.\n&quot;</span>;
      <span class="keywordtype">bool</span> collision = <span class="keyword">false</span>;
      <span class="comment">// if we are listening to the channel and receive anything, there is a collision in the slot.</span>
      <span class="keywordflow">if</span> (checkChannel-&gt;isScheduled())
      {
        cancelEvent(checkChannel);
        collision = <span class="keyword">true</span>;
      }

      <span class="keywordflow">for</span> (<span class="keywordtype">int</span> s = 0; s &lt; <a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a>; s++) {
        <a class="code" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912" title="Occupied slots of two-hop neighbors.">occSlotsAway</a>[s] = mac-&gt;getOccupiedSlots(s);
        debugEV &lt;&lt; <span class="stringliteral">&quot;Occupied slot &quot;</span> &lt;&lt; s &lt;&lt; <span class="stringliteral">&quot;: &quot;</span> &lt;&lt; <a class="code" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912" title="Occupied slots of two-hop neighbors.">occSlotsAway</a>[s] &lt;&lt; endl;
        debugEV &lt;&lt; <span class="stringliteral">&quot;Occupied direct slot &quot;</span> &lt;&lt; s &lt;&lt; <span class="stringliteral">&quot;: &quot;</span> &lt;&lt; <a class="code" href="a00126.html#a26e2b33eae9cef9a2b578173deba9a05" title="Occupied slots from nodes, from which I hear directly.">occSlotsDirect</a>[s] &lt;&lt; endl;
      }

      <span class="keywordflow">if</span> (mac-&gt;getMySlot() &gt;-1)
      {
        <span class="comment">// check first whether this address didn&#39;t have another occupied slot and free it again</span>
        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i &lt; <a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a>; i++)
        {
          <span class="keywordflow">if</span> (<a class="code" href="a00126.html#a26e2b33eae9cef9a2b578173deba9a05" title="Occupied slots from nodes, from which I hear directly.">occSlotsDirect</a>[i] == mac-&gt;getSrcAddr())
            <a class="code" href="a00126.html#a26e2b33eae9cef9a2b578173deba9a05" title="Occupied slots from nodes, from which I hear directly.">occSlotsDirect</a>[i] = LMAC_FREE_SLOT;
          <span class="keywordflow">if</span> (<a class="code" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912" title="Occupied slots of two-hop neighbors.">occSlotsAway</a>[i] == mac-&gt;getSrcAddr())
            <a class="code" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912" title="Occupied slots of two-hop neighbors.">occSlotsAway</a>[i] = LMAC_FREE_SLOT;
        }
        <a class="code" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912" title="Occupied slots of two-hop neighbors.">occSlotsAway</a>[mac-&gt;getMySlot()]   = mac-&gt;getSrcAddr();
        <a class="code" href="a00126.html#a26e2b33eae9cef9a2b578173deba9a05" title="Occupied slots from nodes, from which I hear directly.">occSlotsDirect</a>[mac-&gt;getMySlot()] = mac-&gt;getSrcAddr();
      }
      collision = collision || (mac-&gt;getMySlot() == <a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a>);
      <span class="keywordflow">if</span> (((<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> &gt; -1) &amp;&amp; (mac-&gt;getOccupiedSlots(<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a>) &gt; LMAC_FREE_SLOT) &amp;&amp; (mac-&gt;getOccupiedSlots(<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a>) != <a class="code" href="a00026.html#a8a26df9da07801d8ccb530530ef57ee2" title="MAC address.">myMacAddr</a>)) || collision)
      {
        debugEV &lt;&lt; <span class="stringliteral">&quot;My slot is taken by &quot;</span> &lt;&lt; mac-&gt;getOccupiedSlots(<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a>) &lt;&lt; <span class="stringliteral">&quot;. I need to change it.\n&quot;</span>;
        <a class="code" href="a00126.html#a16e8e67123131834e7a3b4371c518203" title="find a new slot">findNewSlot</a>();
        debugEV &lt;&lt; <span class="stringliteral">&quot;My new slot is &quot;</span> &lt;&lt; <a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> &lt;&lt; endl;
      }
      <span class="keywordflow">if</span> (<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> &lt; 0)
      {
        debugEV &lt;&lt; <span class="stringliteral">&quot;I don;t have a slot - try to find one.\n&quot;</span>;
        <a class="code" href="a00126.html#a16e8e67123131834e7a3b4371c518203" title="find a new slot">findNewSlot</a>();
      }

      <span class="keywordflow">if</span>(dest == <a class="code" href="a00026.html#a8a26df9da07801d8ccb530530ef57ee2" title="MAC address.">myMacAddr</a> || <a class="code" href="a00117.html#a3890f1c7314c5695e32252468e6bd5a5" title="Test if a L2 address (pSrcAddr) is a broadcast address.">LAddress::isL2Broadcast</a>(dest))
      {
        debugEV &lt;&lt; <span class="stringliteral">&quot;I need to stay awake.\n&quot;</span>;
        <span class="keywordflow">if</span> (timeout-&gt;isScheduled())
          cancelEvent(timeout);
        <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a>=WAIT_DATA;
        debugEV &lt;&lt; <span class="stringliteral">&quot;Old state: CCA, New state: WAIT_DATA&quot;</span> &lt;&lt; endl;
      }
      <span class="keywordflow">else</span>
      {
        debugEV &lt;&lt; <span class="stringliteral">&quot;Incoming data packet not for me. Going back to sleep.\n&quot;</span>;
        <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> = SLEEP;
        debugEV &lt;&lt; <span class="stringliteral">&quot;Old state: CCA, New state: SLEEP&quot;</span> &lt;&lt; endl;
        <a class="code" href="a00026.html#aa7b1e6bdb734765a9f4eff7d486dfbea" title="Handler to the physical layer.">phy</a>-&gt;<a class="code" href="a00134.html#ad25abbad9aa16bdbf24bf1dd582b1c6f" title="Tells the BasePhyLayer to switch to the specified radio state.">setRadioState</a>(<a class="code" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7afd684ca0a1c84f153dec831272ccde42" title="sleeping">MiximRadio::SLEEP</a>);
        <span class="keywordflow">if</span> (timeout-&gt;isScheduled())
          cancelEvent(timeout);
      }
      <span class="keyword">delete</span> mac;
    }
    <span class="comment">//probably it never happens</span>
    <span class="keywordflow">else</span> <span class="keywordflow">if</span>(msg-&gt;getKind() == LMAC_DATA)
    {
      LMacPkt *<span class="keyword">const</span>          mac  = <span class="keyword">static_cast&lt;</span>LMacPkt *<span class="keyword">&gt;</span>(msg);
      <span class="keyword">const</span> <a class="code" href="a00117.html#aea56b60dcb5ae8c2bde465271daf7210" title="Type definition for a L2 (MAC) address.">LAddress::L2Type</a>&amp; dest = mac-&gt;getDestAddr();
      <span class="comment">//bool collision = false;</span>
      <span class="comment">// if we are listening to the channel and receive anything, there is a collision in the slot.</span>
      <span class="keywordflow">if</span> (checkChannel-&gt;isScheduled())
      {
        cancelEvent(checkChannel);
        <span class="comment">//collision = true;</span>
      }
      debugEV &lt;&lt; <span class="stringliteral">&quot; I have received a data packet.\n&quot;</span>;
      <span class="keywordflow">if</span>(dest == <a class="code" href="a00026.html#a8a26df9da07801d8ccb530530ef57ee2" title="MAC address.">myMacAddr</a> || <a class="code" href="a00117.html#a3890f1c7314c5695e32252468e6bd5a5" title="Test if a L2 address (pSrcAddr) is a broadcast address.">LAddress::isL2Broadcast</a>(dest))
      {
        debugEV &lt;&lt; <span class="stringliteral">&quot;sending pkt to upper...\n&quot;</span>;
        <a class="code" href="a00025.html#adbf061566606041db811b519f0400ed5" title="Sends a message to the upper layer.">sendUp</a>(<a class="code" href="a00026.html#a998b4c4c56074a354b50f8c4fc5d6d4c" title="decapsulate the network message from the MacPkt">decapsMsg</a>(mac));
      }
      <span class="keywordflow">else</span> {
        debugEV &lt;&lt; <span class="stringliteral">&quot;packet not for me, deleting...\n&quot;</span>;
        <span class="keyword">delete</span> mac;
      }
      <span class="comment">// in any case, go back to sleep</span>
      <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> = SLEEP;
      debugEV &lt;&lt; <span class="stringliteral">&quot;Old state: CCA, New state: SLEEP&quot;</span> &lt;&lt; endl;
      <a class="code" href="a00026.html#aa7b1e6bdb734765a9f4eff7d486dfbea" title="Handler to the physical layer.">phy</a>-&gt;<a class="code" href="a00134.html#ad25abbad9aa16bdbf24bf1dd582b1c6f" title="Tells the BasePhyLayer to switch to the specified radio state.">setRadioState</a>(<a class="code" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7afd684ca0a1c84f153dec831272ccde42" title="sleeping">MiximRadio::SLEEP</a>);
    }
    <span class="keywordflow">else</span> <span class="keywordflow">if</span>(msg-&gt;getKind() == LMAC_SETUP_PHASE_END)
    {
      debugEV &lt;&lt; <span class="stringliteral">&quot;Setup phase end. Start normal work at the next slot.\n&quot;</span>;
      <span class="keywordflow">if</span> (wakeup-&gt;isScheduled())
        cancelEvent(wakeup);

      scheduleAt(simTime()+<a class="code" href="a00126.html#aadcf84520c3dec4d76236cac8fdff135" title="Duration of a slot.">slotDuration</a>, wakeup);

      <a class="code" href="a00126.html#a6c97a461db5cd91f55d3d924b2a184dd" title="the setup phase is the beginning of the simulation, where only control packets at very small slot dur...">SETUP_PHASE</a> = <span class="keyword">false</span>;
    }
    <span class="keywordflow">else</span>
    {
      EV &lt;&lt; <span class="stringliteral">&quot;Unknown packet&quot;</span> &lt;&lt; msg-&gt;getKind() &lt;&lt;  <span class="stringliteral">&quot;in state&quot;</span> &lt;&lt; <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> &lt;&lt; endl;
    }
    <span class="keywordflow">break</span>;

  <span class="keywordflow">case</span> WAIT_CONTROL:
    <span class="keywordflow">if</span>(msg-&gt;getKind() == LMAC_TIMEOUT)
    {
      debugEV &lt;&lt; <span class="stringliteral">&quot;Control timeout. Go back to sleep.\n&quot;</span>;
      <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> = SLEEP;
      debugEV &lt;&lt; <span class="stringliteral">&quot;Old state: WAIT_CONTROL, New state: SLEEP&quot;</span> &lt;&lt; endl;
      <a class="code" href="a00026.html#aa7b1e6bdb734765a9f4eff7d486dfbea" title="Handler to the physical layer.">phy</a>-&gt;<a class="code" href="a00134.html#ad25abbad9aa16bdbf24bf1dd582b1c6f" title="Tells the BasePhyLayer to switch to the specified radio state.">setRadioState</a>(<a class="code" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7afd684ca0a1c84f153dec831272ccde42" title="sleeping">MiximRadio::SLEEP</a>);
    }
    <span class="keywordflow">else</span> <span class="keywordflow">if</span>(msg-&gt;getKind() == LMAC_CONTROL)
    {
      LMacPkt *<span class="keyword">const</span>          mac  = <span class="keyword">static_cast&lt;</span>LMacPkt *<span class="keyword">&gt;</span>(msg);
      <span class="keyword">const</span> <a class="code" href="a00117.html#aea56b60dcb5ae8c2bde465271daf7210" title="Type definition for a L2 (MAC) address.">LAddress::L2Type</a>&amp; dest = mac-&gt;getDestAddr();
      debugEV &lt;&lt; <span class="stringliteral">&quot; I have received a control packet from src &quot;</span> &lt;&lt; mac-&gt;getSrcAddr() &lt;&lt; <span class="stringliteral">&quot; and dest &quot;</span> &lt;&lt; dest &lt;&lt; <span class="stringliteral">&quot;.\n&quot;</span>;

      <span class="keywordtype">bool</span> collision = <span class="keyword">false</span>;

      <span class="comment">// check first the slot assignment</span>
      <span class="comment">// copy the current slot assignment</span>

      <span class="keywordflow">for</span> (<span class="keywordtype">int</span> s = 0; s &lt; <a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a>; s++)
      {
        <a class="code" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912" title="Occupied slots of two-hop neighbors.">occSlotsAway</a>[s] = mac-&gt;getOccupiedSlots(s);
        debugEV &lt;&lt; <span class="stringliteral">&quot;Occupied slot &quot;</span> &lt;&lt; s &lt;&lt; <span class="stringliteral">&quot;: &quot;</span> &lt;&lt; <a class="code" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912" title="Occupied slots of two-hop neighbors.">occSlotsAway</a>[s] &lt;&lt; endl;
        debugEV &lt;&lt; <span class="stringliteral">&quot;Occupied direct slot &quot;</span> &lt;&lt; s &lt;&lt; <span class="stringliteral">&quot;: &quot;</span> &lt;&lt; <a class="code" href="a00126.html#a26e2b33eae9cef9a2b578173deba9a05" title="Occupied slots from nodes, from which I hear directly.">occSlotsDirect</a>[s] &lt;&lt; endl;
      }

      <span class="keywordflow">if</span> (mac-&gt;getMySlot() &gt;-1)
      {
        <span class="comment">// check first whether this address didn&#39;t have another occupied slot and free it again</span>
        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i &lt; <a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a>; i++)
        {
          <span class="keywordflow">if</span> (<a class="code" href="a00126.html#a26e2b33eae9cef9a2b578173deba9a05" title="Occupied slots from nodes, from which I hear directly.">occSlotsDirect</a>[i] == mac-&gt;getSrcAddr())
            <a class="code" href="a00126.html#a26e2b33eae9cef9a2b578173deba9a05" title="Occupied slots from nodes, from which I hear directly.">occSlotsDirect</a>[i] = LMAC_FREE_SLOT;
          <span class="keywordflow">if</span> (<a class="code" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912" title="Occupied slots of two-hop neighbors.">occSlotsAway</a>[i] == mac-&gt;getSrcAddr())
            <a class="code" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912" title="Occupied slots of two-hop neighbors.">occSlotsAway</a>[i] = LMAC_FREE_SLOT;
        }
        <a class="code" href="a00126.html#a28d9565b4b0a059ce0826e2610afb912" title="Occupied slots of two-hop neighbors.">occSlotsAway</a>[mac-&gt;getMySlot()]   = mac-&gt;getSrcAddr();
        <a class="code" href="a00126.html#a26e2b33eae9cef9a2b578173deba9a05" title="Occupied slots from nodes, from which I hear directly.">occSlotsDirect</a>[mac-&gt;getMySlot()] = mac-&gt;getSrcAddr();
      }

      collision = collision || (mac-&gt;getMySlot() == <a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a>);
      <span class="keywordflow">if</span> (((<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> &gt; -1) &amp;&amp; (mac-&gt;getOccupiedSlots(<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a>) &gt; LMAC_FREE_SLOT) &amp;&amp; (mac-&gt;getOccupiedSlots(<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a>) != <a class="code" href="a00026.html#a8a26df9da07801d8ccb530530ef57ee2" title="MAC address.">myMacAddr</a>)) || collision)
      {
        debugEV &lt;&lt; <span class="stringliteral">&quot;My slot is taken by &quot;</span> &lt;&lt; mac-&gt;getOccupiedSlots(<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a>) &lt;&lt; <span class="stringliteral">&quot;. I need to change it.\n&quot;</span>;
        <a class="code" href="a00126.html#a16e8e67123131834e7a3b4371c518203" title="find a new slot">findNewSlot</a>();
        debugEV &lt;&lt; <span class="stringliteral">&quot;My new slot is &quot;</span> &lt;&lt; <a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> &lt;&lt; endl;
      }
      <span class="keywordflow">if</span> (<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> &lt; 0)
      {
        debugEV &lt;&lt; <span class="stringliteral">&quot;I don;t have a slot - try to find one.\n&quot;</span>;
        <a class="code" href="a00126.html#a16e8e67123131834e7a3b4371c518203" title="find a new slot">findNewSlot</a>();
      }

      <span class="keywordflow">if</span>(dest == <a class="code" href="a00026.html#a8a26df9da07801d8ccb530530ef57ee2" title="MAC address.">myMacAddr</a> || <a class="code" href="a00117.html#a3890f1c7314c5695e32252468e6bd5a5" title="Test if a L2 address (pSrcAddr) is a broadcast address.">LAddress::isL2Broadcast</a>(dest))
      {
        debugEV &lt;&lt; <span class="stringliteral">&quot;I need to stay awake.\n&quot;</span>;
        <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a>=WAIT_DATA;
        debugEV &lt;&lt; <span class="stringliteral">&quot;Old state: WAIT_CONTROL, New state: WAIT_DATA&quot;</span> &lt;&lt; endl;
        <span class="keywordflow">if</span> (timeout-&gt;isScheduled())
          cancelEvent(timeout);
      }
      <span class="keywordflow">else</span>
      {
        debugEV &lt;&lt; <span class="stringliteral">&quot;Incoming data packet not for me. Going back to sleep.\n&quot;</span>;
        <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> = SLEEP;
        debugEV &lt;&lt; <span class="stringliteral">&quot;Old state: WAIT_CONTROL, New state: SLEEP&quot;</span> &lt;&lt; endl;
        <a class="code" href="a00026.html#aa7b1e6bdb734765a9f4eff7d486dfbea" title="Handler to the physical layer.">phy</a>-&gt;<a class="code" href="a00134.html#ad25abbad9aa16bdbf24bf1dd582b1c6f" title="Tells the BasePhyLayer to switch to the specified radio state.">setRadioState</a>(<a class="code" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7afd684ca0a1c84f153dec831272ccde42" title="sleeping">MiximRadio::SLEEP</a>);
        <span class="keywordflow">if</span> (timeout-&gt;isScheduled())
          cancelEvent(timeout);
      }
      <span class="keyword">delete</span> mac;
    }
    <span class="keywordflow">else</span> <span class="keywordflow">if</span> ((msg-&gt;getKind() == LMAC_WAKEUP))
    {
      <span class="keywordflow">if</span> (<a class="code" href="a00126.html#a6c97a461db5cd91f55d3d924b2a184dd" title="the setup phase is the beginning of the simulation, where only control packets at very small slot dur...">SETUP_PHASE</a> == <span class="keyword">true</span>)
        debugEV &lt;&lt; <span class="stringliteral">&quot;End of setup-phase slot&quot;</span> &lt;&lt; endl;
      <span class="keywordflow">else</span>
        debugEV &lt;&lt; <span class="stringliteral">&quot;Very unlikely transition&quot;</span>;

      <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> = SLEEP;
      debugEV &lt;&lt; <span class="stringliteral">&quot;Old state: WAIT_DATA, New state: SLEEP&quot;</span> &lt;&lt; endl;
      scheduleAt(simTime(), wakeup);

    }
    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (msg-&gt;getKind() == LMAC_SETUP_PHASE_END)
    {
      debugEV &lt;&lt; <span class="stringliteral">&quot;Setup phase end. Start normal work at the next slot.\n&quot;</span>;
      <span class="keywordflow">if</span> (wakeup-&gt;isScheduled())
        cancelEvent(wakeup);

      scheduleAt(simTime()+<a class="code" href="a00126.html#aadcf84520c3dec4d76236cac8fdff135" title="Duration of a slot.">slotDuration</a>, wakeup);

      <a class="code" href="a00126.html#a6c97a461db5cd91f55d3d924b2a184dd" title="the setup phase is the beginning of the simulation, where only control packets at very small slot dur...">SETUP_PHASE</a> = <span class="keyword">false</span>;
    }
    <span class="keywordflow">else</span>
    {
      EV &lt;&lt; <span class="stringliteral">&quot;Unknown packet&quot;</span> &lt;&lt; msg-&gt;getKind() &lt;&lt;  <span class="stringliteral">&quot;in state&quot;</span> &lt;&lt; <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> &lt;&lt; endl;
    }

    <span class="keywordflow">break</span>;

  <span class="keywordflow">case</span> SEND_CONTROL:

    <span class="keywordflow">if</span>(msg-&gt;getKind() == LMAC_SEND_CONTROL)
    {
      <span class="comment">// send first a control message, so that non-receiving nodes can switch off.</span>
      coreEV &lt;&lt; <span class="stringliteral">&quot;Sending a control packet.\n&quot;</span>;
      LMacPkt* control = <span class="keyword">new</span> LMacPkt();
      control-&gt;setKind(LMAC_CONTROL);
      <span class="keywordflow">if</span> ((<a class="code" href="a00126.html#a4d4d0764f3a0b35fce09f71fa334ede5" title="A queue to store packets from upper layer in case another packet is still waiting for transmission...">macQueue</a>.size() &gt; 0) &amp;&amp; !<a class="code" href="a00126.html#a6c97a461db5cd91f55d3d924b2a184dd" title="the setup phase is the beginning of the simulation, where only control packets at very small slot dur...">SETUP_PHASE</a>)
        control-&gt;setDestAddr((<a class="code" href="a00126.html#a4d4d0764f3a0b35fce09f71fa334ede5" title="A queue to store packets from upper layer in case another packet is still waiting for transmission...">macQueue</a>.front())-&gt;getDestAddr());
      <span class="keywordflow">else</span>
        control-&gt;setDestAddr(<a class="code" href="a00126.html#a04b4749559221c7d8f886e01ecc123e6" title="dummy receiver address to indicate no pending packets in the control packet">LMAC_NO_RECEIVER</a>);

      control-&gt;setSrcAddr(<a class="code" href="a00026.html#a8a26df9da07801d8ccb530530ef57ee2" title="MAC address.">myMacAddr</a>);
      control-&gt;setMySlot(<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a>);
      control-&gt;setBitLength(<a class="code" href="a00026.html#a26ddb186455b73c8dc8b5698296804d2" title="Pointer to the arp module.">headerLength</a> + numSlots);
      control-&gt;setOccupiedSlotsArraySize(numSlots);
      <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a>; i++)
        control-&gt;setOccupiedSlots(i, <a class="code" href="a00126.html#a26e2b33eae9cef9a2b578173deba9a05" title="Occupied slots from nodes, from which I hear directly.">occSlotsDirect</a>[i]);

      <a class="code" href="a00126.html#a14db63191f60dc3f3b906ce02eec6628" title="Internal function to attach a signal to the packet.">attachSignal</a>(control);
      <a class="code" href="a00025.html#a1873d6e3fc6d92cf8482b39f1c5ac6c8" title="Sends a message to the lower layer.">sendDown</a>(control);
      <span class="keywordflow">if</span> ((<a class="code" href="a00126.html#a4d4d0764f3a0b35fce09f71fa334ede5" title="A queue to store packets from upper layer in case another packet is still waiting for transmission...">macQueue</a>.size() &gt; 0) &amp;&amp; (!<a class="code" href="a00126.html#a6c97a461db5cd91f55d3d924b2a184dd" title="the setup phase is the beginning of the simulation, where only control packets at very small slot dur...">SETUP_PHASE</a>))
        scheduleAt(simTime()+<a class="code" href="a00126.html#a3bcd0ad076a7868e7d8ae0e591d46fec" title="Duration of teh control time in each slot.">controlDuration</a>, sendData);
    }
    <span class="keywordflow">else</span> <span class="keywordflow">if</span>(msg-&gt;getKind() == LMAC_SEND_DATA)
    {
      <span class="comment">// we should be in our own slot and the control packet should be already sent. receiving neighbors should wait for the data now.</span>
      <span class="keywordflow">if</span> (<a class="code" href="a00126.html#aee8d9b764a36dd71fc189cbf6ef56d6e" title="The current slot of the simulation.">currSlot</a> != <a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a>)
      {
        debugEV &lt;&lt; <span class="stringliteral">&quot;ERROR: Send data message received, but we are not in our slot!!! Repair.\n&quot;</span>;
        <a class="code" href="a00026.html#aa7b1e6bdb734765a9f4eff7d486dfbea" title="Handler to the physical layer.">phy</a>-&gt;<a class="code" href="a00134.html#ad25abbad9aa16bdbf24bf1dd582b1c6f" title="Tells the BasePhyLayer to switch to the specified radio state.">setRadioState</a>(<a class="code" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7afd684ca0a1c84f153dec831272ccde42" title="sleeping">MiximRadio::SLEEP</a>);
        <span class="keywordflow">if</span> (timeout-&gt;isScheduled())
          cancelEvent(timeout);
        <span class="keywordflow">return</span>;
      }
      LMacPkt* data = <a class="code" href="a00126.html#a4d4d0764f3a0b35fce09f71fa334ede5" title="A queue to store packets from upper layer in case another packet is still waiting for transmission...">macQueue</a>.front()-&gt;dup();
      data-&gt;setKind(LMAC_DATA);
      data-&gt;setMySlot(<a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a>);
      data-&gt;setOccupiedSlotsArraySize(numSlots);
      <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a>; i++)
        data-&gt;setOccupiedSlots(i, <a class="code" href="a00126.html#a26e2b33eae9cef9a2b578173deba9a05" title="Occupied slots from nodes, from which I hear directly.">occSlotsDirect</a>[i]);

      <a class="code" href="a00126.html#a14db63191f60dc3f3b906ce02eec6628" title="Internal function to attach a signal to the packet.">attachSignal</a>(data);
      coreEV &lt;&lt; <span class="stringliteral">&quot;Sending down data packet\n&quot;</span>;
      <a class="code" href="a00025.html#a1873d6e3fc6d92cf8482b39f1c5ac6c8" title="Sends a message to the lower layer.">sendDown</a>(data);
      <span class="keyword">delete</span> <a class="code" href="a00126.html#a4d4d0764f3a0b35fce09f71fa334ede5" title="A queue to store packets from upper layer in case another packet is still waiting for transmission...">macQueue</a>.front();
      <a class="code" href="a00126.html#a4d4d0764f3a0b35fce09f71fa334ede5" title="A queue to store packets from upper layer in case another packet is still waiting for transmission...">macQueue</a>.pop_front();
      <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> = SEND_DATA;
      debugEV &lt;&lt; <span class="stringliteral">&quot;Old state: SEND_CONTROL, New state: SEND_DATA&quot;</span> &lt;&lt; endl;
    }
    <span class="keywordflow">else</span> <span class="keywordflow">if</span>(msg-&gt;getKind() == LMAC_SETUP_PHASE_END)
    {
      debugEV &lt;&lt; <span class="stringliteral">&quot;Setup phase end. Start normal work at the next slot.\n&quot;</span>;
      <span class="keywordflow">if</span> (wakeup-&gt;isScheduled())
        cancelEvent(wakeup);

      scheduleAt(simTime()+<a class="code" href="a00126.html#aadcf84520c3dec4d76236cac8fdff135" title="Duration of a slot.">slotDuration</a>, wakeup);

      <a class="code" href="a00126.html#a6c97a461db5cd91f55d3d924b2a184dd" title="the setup phase is the beginning of the simulation, where only control packets at very small slot dur...">SETUP_PHASE</a> = <span class="keyword">false</span>;
    }
    <span class="keywordflow">else</span>
    {
      EV &lt;&lt; <span class="stringliteral">&quot;Unknown packet&quot;</span> &lt;&lt; msg-&gt;getKind() &lt;&lt;  <span class="stringliteral">&quot;in state&quot;</span> &lt;&lt; <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> &lt;&lt; endl;
    }
    <span class="keywordflow">break</span>;

  <span class="keywordflow">case</span> SEND_DATA:
    <span class="keywordflow">if</span>(msg-&gt;getKind() == LMAC_WAKEUP)
    {
      error(<span class="stringliteral">&quot;I am still sending a message, while a new slot is starting!\n&quot;</span>);
    }
    <span class="keywordflow">else</span>
    {
      EV &lt;&lt; <span class="stringliteral">&quot;Unknown packet&quot;</span> &lt;&lt; msg-&gt;getKind() &lt;&lt;  <span class="stringliteral">&quot;in state&quot;</span> &lt;&lt; <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> &lt;&lt; endl;
    }
    <span class="keywordflow">break</span>;

  <span class="keywordflow">case</span> WAIT_DATA:
    <span class="keywordflow">if</span>(msg-&gt;getKind() == LMAC_DATA)
    {
      LMacPkt *<span class="keyword">const</span>          mac  = <span class="keyword">static_cast&lt;</span>LMacPkt *<span class="keyword">&gt;</span>(msg);
      <span class="keyword">const</span> <a class="code" href="a00117.html#aea56b60dcb5ae8c2bde465271daf7210" title="Type definition for a L2 (MAC) address.">LAddress::L2Type</a>&amp; dest = mac-&gt;getDestAddr();

      debugEV &lt;&lt; <span class="stringliteral">&quot; I have received a data packet.\n&quot;</span>;
      <span class="keywordflow">if</span>(dest == <a class="code" href="a00026.html#a8a26df9da07801d8ccb530530ef57ee2" title="MAC address.">myMacAddr</a> || <a class="code" href="a00117.html#a3890f1c7314c5695e32252468e6bd5a5" title="Test if a L2 address (pSrcAddr) is a broadcast address.">LAddress::isL2Broadcast</a>(dest))
      {
        debugEV &lt;&lt; <span class="stringliteral">&quot;sending pkt to upper...\n&quot;</span>;
        <a class="code" href="a00025.html#adbf061566606041db811b519f0400ed5" title="Sends a message to the upper layer.">sendUp</a>(<a class="code" href="a00026.html#a998b4c4c56074a354b50f8c4fc5d6d4c" title="decapsulate the network message from the MacPkt">decapsMsg</a>(mac));
      }
      <span class="keywordflow">else</span> {
        debugEV &lt;&lt; <span class="stringliteral">&quot;packet not for me, deleting...\n&quot;</span>;
        <span class="keyword">delete</span> mac;
      }
      <span class="comment">// in any case, go back to sleep</span>
      <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> = SLEEP;
      debugEV &lt;&lt; <span class="stringliteral">&quot;Old state: WAIT_DATA, New state: SLEEP&quot;</span> &lt;&lt; endl;
      <a class="code" href="a00026.html#aa7b1e6bdb734765a9f4eff7d486dfbea" title="Handler to the physical layer.">phy</a>-&gt;<a class="code" href="a00134.html#ad25abbad9aa16bdbf24bf1dd582b1c6f" title="Tells the BasePhyLayer to switch to the specified radio state.">setRadioState</a>(<a class="code" href="a00141.html#a5d69aacb1cf6a57827f07224219fccb7afd684ca0a1c84f153dec831272ccde42" title="sleeping">MiximRadio::SLEEP</a>);
      <span class="keywordflow">if</span> (timeout-&gt;isScheduled())
        cancelEvent(timeout);
    }
    <span class="keywordflow">else</span> <span class="keywordflow">if</span>(msg-&gt;getKind() == LMAC_WAKEUP)
    {
      <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> = SLEEP;
      debugEV &lt;&lt; <span class="stringliteral">&quot;Unlikely transition. Old state: WAIT_DATA, New state: SLEEP&quot;</span> &lt;&lt; endl;
      scheduleAt(simTime(), wakeup);
    }
    <span class="keywordflow">else</span>
    {
      EV &lt;&lt; <span class="stringliteral">&quot;Unknown packet&quot;</span> &lt;&lt; msg-&gt;getKind() &lt;&lt;  <span class="stringliteral">&quot;in state&quot;</span> &lt;&lt; <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> &lt;&lt; endl;
    }
    <span class="keywordflow">break</span>;
  <span class="keywordflow">default</span>:
    opp_error(<span class="stringliteral">&quot;Unknown mac state: %d&quot;</span>, <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a>);
    <span class="keywordflow">break</span>;
  }
}
</pre></div>
</div>
</div>
<a class="anchor" id="a7de5ee6f865e3ae8e7a9841291d36a37"></a><!-- doxytag: member="LMacLayer::handleUpperMsg" ref="a7de5ee6f865e3ae8e7a9841291d36a37" args="(cMessage *)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void LMacLayer::handleUpperMsg </td>
          <td>(</td>
          <td class="paramtype">cMessage *&#160;</td>
          <td class="paramname"><em>msg</em></td><td>)</td>
          <td><code> [virtual]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>Handle messages from upper layer. </p>
<p>Check whether the queue is not full: if yes, print a warning and drop the packet. Sending of messages is automatic. </p>

<p>Reimplemented from <a class="el" href="a00026.html#a6de2ca07018de313a7147a03e6d77bff">BaseMacLayer</a>.</p>

<p>References <a class="el" href="a00025.html#ad8ba02d92dd106da1d85192eb28250c0">BaseLayer::catDroppedPacketSignal</a>, <a class="el" href="a00126.html#aee8d9b764a36dd71fc189cbf6ef56d6e">currSlot</a>, <a class="el" href="a00126.html#a5631de1b0bad61ff8c6366040cc4681c">droppedPacket</a>, <a class="el" href="a00126.html#a85b92d5d2292228cb408f4e72fca7caf">encapsMsg()</a>, <a class="el" href="a00126.html#a4d4d0764f3a0b35fce09f71fa334ede5">macQueue</a>, <a class="el" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007">macState</a>, <a class="el" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d">mySlot</a>, <a class="el" href="a00026.html#a49e0e548a6c20d002bd67afa7b76c483adebf9c77bbe6f19772866cccbef0fe4b">BaseMacLayer::PACKET_DROPPED</a>, <a class="el" href="a00126.html#aa13209c2a38ad4cb43f4c73a3283e55c">queueLength</a>, <a class="el" href="a00025.html#aa1190a06fb4dfb15d8ff001468335dc8">BaseLayer::sendControlUp()</a>, and <a class="el" href="a00088.html#aef3c55476a292fea6434cc1c4c27f822">DroppedPacket::setReason()</a>.</p>
<div class="fragment"><pre class="fragment">{
    LMacPkt *mac = <span class="keyword">static_cast&lt;</span>LMacPkt *<span class="keyword">&gt;</span>(<a class="code" href="a00126.html#a85b92d5d2292228cb408f4e72fca7caf" title="Encapsulate the NetwPkt into an MacPkt.">encapsMsg</a>(static_cast&lt;cPacket*&gt;(msg)));

    <span class="comment">// message has to be queued if another message is waiting to be send</span>
    <span class="comment">// or if we are already trying to send another message</span>

    <span class="keywordflow">if</span> (<a class="code" href="a00126.html#a4d4d0764f3a0b35fce09f71fa334ede5" title="A queue to store packets from upper layer in case another packet is still waiting for transmission...">macQueue</a>.size() &lt;= <a class="code" href="a00126.html#aa13209c2a38ad4cb43f4c73a3283e55c" title="length of the queue">queueLength</a>) {
        <a class="code" href="a00126.html#a4d4d0764f3a0b35fce09f71fa334ede5" title="A queue to store packets from upper layer in case another packet is still waiting for transmission...">macQueue</a>.push_back(mac);
  debugEV &lt;&lt; <span class="stringliteral">&quot;packet put in queue\n  queue size: &quot;</span> &lt;&lt; <a class="code" href="a00126.html#a4d4d0764f3a0b35fce09f71fa334ede5" title="A queue to store packets from upper layer in case another packet is still waiting for transmission...">macQueue</a>.size() &lt;&lt; <span class="stringliteral">&quot; macState: &quot;</span> &lt;&lt; <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a>
      &lt;&lt; <span class="stringliteral">&quot;; mySlot is &quot;</span> &lt;&lt; <a class="code" href="a00126.html#a86d7beca6b3521cb80b44cd8c239b48d" title="my slot ID">mySlot</a> &lt;&lt; <span class="stringliteral">&quot;; current slot is &quot;</span> &lt;&lt; <a class="code" href="a00126.html#aee8d9b764a36dd71fc189cbf6ef56d6e" title="The current slot of the simulation.">currSlot</a> &lt;&lt; endl;;

    }
    <span class="keywordflow">else</span> {
        <span class="comment">// queue is full, message has to be deleted</span>
        debugEV &lt;&lt; <span class="stringliteral">&quot;New packet arrived, but queue is FULL, so new packet is deleted\n&quot;</span>;
        mac-&gt;setName(<span class="stringliteral">&quot;MAC ERROR&quot;</span>);
        mac-&gt;setKind(<a class="code" href="a00026.html#a49e0e548a6c20d002bd67afa7b76c483adebf9c77bbe6f19772866cccbef0fe4b">PACKET_DROPPED</a>);
        <a class="code" href="a00025.html#aa1190a06fb4dfb15d8ff001468335dc8" title="Sends a control message to an upper layer.">sendControlUp</a>(mac);
        <a class="code" href="a00126.html#a5631de1b0bad61ff8c6366040cc4681c" title="Inspect reasons for dropped packets.">droppedPacket</a>.<a class="code" href="a00088.html#aef3c55476a292fea6434cc1c4c27f822" title="set the state of the radio">setReason</a>(DroppedPacket::QUEUE);
        emit(<a class="code" href="a00025.html#ad8ba02d92dd106da1d85192eb28250c0" title="Signal for dropped packets.">BaseLayer::catDroppedPacketSignal</a>, &amp;<a class="code" href="a00126.html#a5631de1b0bad61ff8c6366040cc4681c" title="Inspect reasons for dropped packets.">droppedPacket</a>);
        debugEV &lt;&lt;  <span class="stringliteral">&quot;ERROR: Queue is full, forced to delete.\n&quot;</span>;
    }
}
</pre></div>
</div>
</div>
<a class="anchor" id="a2100ca7de5c5eedb3e7f0e11ca9fca14"></a><!-- doxytag: member="LMacLayer::initialize" ref="a2100ca7de5c5eedb3e7f0e11ca9fca14" args="(int)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void LMacLayer::initialize </td>
          <td>(</td>
          <td class="paramtype">int&#160;</td>
          <td class="paramname"><em>stage</em></td><td>)</td>
          <td><code> [virtual]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>Initialization of the module and some variables. </p>
<p>Initialize the of the omnetpp.ini variables in stage 1. In stage two subscribe to the RadioState. </p>

<p>Reimplemented from <a class="el" href="a00026.html#a61c82dc3b225eac37f7fa9936c28c2c3">BaseMacLayer</a>.</p>

<p>References <a class="el" href="a00126.html#ab797f8cfd8e9b42c7ca72ed33c4af8d8">bitrate</a>, <a class="el" href="a00025.html#ad8ba02d92dd106da1d85192eb28250c0">BaseLayer::catDroppedPacketSignal</a>, <a class="el" href="a00126.html#a3bcd0ad076a7868e7d8ae0e591d46fec">controlDuration</a>, <a class="el" href="a00126.html#a5631de1b0bad61ff8c6366040cc4681c">droppedPacket</a>, <a class="el" href="a00026.html#ae51d8e3e0977cfc1d4f10f8135378fc4">BaseMacLayer::getNic()</a>, <a class="el" href="a00026.html#a26ddb186455b73c8dc8b5698296804d2">BaseMacLayer::headerLength</a>, <a class="el" href="a00201.html#aa01f017b96087bc252445206777e0080">simsignalwrap_t::initialize()</a>, <a class="el" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007">macState</a>, <a class="el" href="a00026.html#a8a26df9da07801d8ccb530530ef57ee2">BaseMacLayer::myMacAddr</a>, <a class="el" href="a00126.html#a64482bc131ae8b5598fbf97db66f4552">nicId</a>, <a class="el" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c">numSlots</a>, <a class="el" href="a00126.html#aa13209c2a38ad4cb43f4c73a3283e55c">queueLength</a>, <a class="el" href="a00126.html#aa5508d9b952be702c8a4d33d63dc517c">reservedMobileSlots</a>, <a class="el" href="a00088.html#aef3c55476a292fea6434cc1c4c27f822">DroppedPacket::setReason()</a>, <a class="el" href="a00126.html#aae582a227411bd299295dec277ab7ad0">slotChange</a>, <a class="el" href="a00126.html#aadcf84520c3dec4d76236cac8fdff135">slotDuration</a>, and <a class="el" href="a00126.html#abe6c59398a7e27aa3e421e65690ed6e8">txPower</a>.</p>
<div class="fragment"><pre class="fragment">{
    <a class="code" href="a00126.html#a2100ca7de5c5eedb3e7f0e11ca9fca14" title="Initialization of the module and some variables.">BaseMacLayer::initialize</a>(stage);

    <span class="keywordflow">if</span> (stage == 0) {
      <a class="code" href="a00025.html#ad8ba02d92dd106da1d85192eb28250c0" title="Signal for dropped packets.">BaseLayer::catDroppedPacketSignal</a>.<a class="code" href="a00201.html#aa01f017b96087bc252445206777e0080">initialize</a>();

        <a class="code" href="a00126.html#aa13209c2a38ad4cb43f4c73a3283e55c" title="length of the queue">queueLength</a> = par(<span class="stringliteral">&quot;queueLength&quot;</span>);
        <a class="code" href="a00126.html#aadcf84520c3dec4d76236cac8fdff135" title="Duration of a slot.">slotDuration</a> = par(<span class="stringliteral">&quot;slotDuration&quot;</span>);
        <a class="code" href="a00126.html#ab797f8cfd8e9b42c7ca72ed33c4af8d8" title="the bit rate at which we transmit">bitrate</a> = par(<span class="stringliteral">&quot;bitrate&quot;</span>);
        <a class="code" href="a00026.html#a26ddb186455b73c8dc8b5698296804d2" title="Pointer to the arp module.">headerLength</a> = par(<span class="stringliteral">&quot;headerLength&quot;</span>);
        coreEV &lt;&lt; <span class="stringliteral">&quot;headerLength is: &quot;</span> &lt;&lt; <a class="code" href="a00026.html#a26ddb186455b73c8dc8b5698296804d2" title="Pointer to the arp module.">headerLength</a> &lt;&lt; endl;
        <a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a> = par(<span class="stringliteral">&quot;numSlots&quot;</span>);
        <span class="comment">// the first N slots are reserved for mobile nodes to be able to function normally</span>
        <a class="code" href="a00126.html#aa5508d9b952be702c8a4d33d63dc517c" title="The first couple of slots are reserved for nodes with special needs to avoid changing slots for them ...">reservedMobileSlots</a> = par(<span class="stringliteral">&quot;reservedMobileSlots&quot;</span>);
        <a class="code" href="a00126.html#abe6c59398a7e27aa3e421e65690ed6e8" title="Transmission power of the node.">txPower</a> = par(<span class="stringliteral">&quot;txPower&quot;</span>);

        <a class="code" href="a00126.html#a5631de1b0bad61ff8c6366040cc4681c" title="Inspect reasons for dropped packets.">droppedPacket</a>.<a class="code" href="a00088.html#aef3c55476a292fea6434cc1c4c27f822" title="set the state of the radio">setReason</a>(DroppedPacket::NONE);
        <a class="code" href="a00126.html#a64482bc131ae8b5598fbf97db66f4552" title="publish dropped packets nic wide">nicId</a> = <a class="code" href="a00026.html#ae51d8e3e0977cfc1d4f10f8135378fc4" title="Pointer to nic Module.">getNic</a>()-&gt;getId();
        debugEV &lt;&lt; <span class="stringliteral">&quot;My Mac address is&quot;</span> &lt;&lt; <a class="code" href="a00026.html#a8a26df9da07801d8ccb530530ef57ee2" title="MAC address.">myMacAddr</a> &lt;&lt; <span class="stringliteral">&quot; and my Id is &quot;</span> &lt;&lt; myId &lt;&lt; endl;

        <a class="code" href="a00126.html#a48ad85f4aaba675b8f5287ad2bdd6007" title="keep track of MAC state">macState</a> = INIT;

        <a class="code" href="a00126.html#aae582a227411bd299295dec277ab7ad0" title="indicate how often the node needs to change its slot because of collisions">slotChange</a> = <span class="keyword">new</span> cOutVector(<span class="stringliteral">&quot;slotChange&quot;</span>);

        <span class="comment">// how long does it take to send/receive a control packet</span>
        <a class="code" href="a00126.html#a3bcd0ad076a7868e7d8ae0e591d46fec" title="Duration of teh control time in each slot.">controlDuration</a> = ((double)<a class="code" href="a00026.html#a26ddb186455b73c8dc8b5698296804d2" title="Pointer to the arp module.">headerLength</a> + (<span class="keywordtype">double</span>)<a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a> + 16) / (<span class="keywordtype">double</span>)<a class="code" href="a00126.html#ab797f8cfd8e9b42c7ca72ed33c4af8d8" title="the bit rate at which we transmit">bitrate</a>;
        coreEV &lt;&lt; <span class="stringliteral">&quot;Control packets take : &quot;</span> &lt;&lt; <a class="code" href="a00126.html#a3bcd0ad076a7868e7d8ae0e591d46fec" title="Duration of teh control time in each slot.">controlDuration</a> &lt;&lt; <span class="stringliteral">&quot; seconds to transmit\n&quot;</span>;
    }

    <span class="keywordflow">else</span> <span class="keywordflow">if</span>(stage == 1) {
        <span class="comment">//int channel;</span>
        <span class="comment">//channel = hasPar(&quot;defaultChannel&quot;) ? par(&quot;defaultChannel&quot;) : 0;</span>

        debugEV &lt;&lt; <span class="stringliteral">&quot;queueLength = &quot;</span> &lt;&lt; <a class="code" href="a00126.html#aa13209c2a38ad4cb43f4c73a3283e55c" title="length of the queue">queueLength</a>
           &lt;&lt; <span class="stringliteral">&quot; slotDuration = &quot;</span> &lt;&lt; <a class="code" href="a00126.html#aadcf84520c3dec4d76236cac8fdff135" title="Duration of a slot.">slotDuration</a>
           &lt;&lt; <span class="stringliteral">&quot; controlDuration = &quot;</span> &lt;&lt; <a class="code" href="a00126.html#a3bcd0ad076a7868e7d8ae0e591d46fec" title="Duration of teh control time in each slot.">controlDuration</a>
           &lt;&lt; <span class="stringliteral">&quot; numSlots = &quot;</span> &lt;&lt; <a class="code" href="a00126.html#a5182c67d55642bb0789bed7222a9e36c" title="how many slots are there">numSlots</a>
           &lt;&lt; <span class="stringliteral">&quot; bitrate = &quot;</span> &lt;&lt; <a class="code" href="a00126.html#ab797f8cfd8e9b42c7ca72ed33c4af8d8" title="the bit rate at which we transmit">bitrate</a> &lt;&lt; endl;

        timeout = <span class="keyword">new</span> cMessage(<span class="stringliteral">&quot;timeout&quot;</span>);
        timeout-&gt;setKind(LMAC_TIMEOUT);

        sendData = <span class="keyword">new</span> cMessage(<span class="stringliteral">&quot;sendData&quot;</span>);
        sendData-&gt;setKind(LMAC_SEND_DATA);

        wakeup = <span class="keyword">new</span> cMessage(<span class="stringliteral">&quot;wakeup&quot;</span>);
        wakeup-&gt;setKind(LMAC_WAKEUP);

        initChecker = <span class="keyword">new</span> cMessage(<span class="stringliteral">&quot;setup phase&quot;</span>);
        initChecker-&gt;setKind(LMAC_SETUP_PHASE_END);

        checkChannel = <span class="keyword">new</span> cMessage(<span class="stringliteral">&quot;checkchannel&quot;</span>);
        checkChannel-&gt;setKind(LMAC_CHECK_CHANNEL);

        start_lmac = <span class="keyword">new</span> cMessage(<span class="stringliteral">&quot;start_lmac&quot;</span>);
        start_lmac-&gt;setKind(LMAC_START_LMAC);

        send_control = <span class="keyword">new</span> cMessage(<span class="stringliteral">&quot;send_control&quot;</span>);
        send_control-&gt;setKind(LMAC_SEND_CONTROL);

        scheduleAt(0.0, start_lmac);

    }
}
</pre></div>
</div>
</div>
<hr/>The documentation for this class was generated from the following files:<ul>
<li>D:/Projects/MiXiM/extsrc/mixim/src/modules/mac/<a class="el" href="a00432_source.html">LMacLayer.h</a></li>
<li>D:/Projects/MiXiM/extsrc/mixim/src/modules/mac/LMacLayer.cc</li>
</ul>
</div>
</div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><a class="el" href="a00126.html">LMacLayer</a>      </li>

    <li class="footer">Generated on Tue Mar 5 2013 21:26:32 for MiXiM by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
